// miniprogram/pages/piece/piece.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        select:[],
        selectlen: 0,
        loading:true
    },  
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },  
    addSelect(e){
        let arr = this.data.select;
        if (this.data.select.indexOf(e.currentTarget.dataset.name) >= 0){
            arr.splice(this.data.select.indexOf(e.currentTarget.dataset.name),1)
        }else{
            arr.push(e.currentTarget.dataset.name)
        }
        this.setData({
            select: arr,
            selectlen: arr.length
        })

        let data = this.data.role;
        let fdata = this.data.role.filter((e)=>{
            return ((arr.indexOf(e.race[0]) >= 0) || (e.race[1] ? arr.indexOf(e.race[1]) >= 0 : false) || (arr.indexOf(e.profession) >= 0))
        })
        this.setData({
            showrole: fdata
        })
    },
    clear(){
        this.setData({
            select: [],
            selectlen: 0,
            showrole: this.data.role
        })
    },
    tabSelect(e) {
        this.setData({
            TabCur: e.currentTarget.dataset.id,
            scrollLeft: (e.currentTarget.dataset.id - 1) * 60
        })
    },
    getTempFileURL: function (list) {
        var that = this;
        if (!list) {
            return false
        }
        if (list.length >= 50) {
            wx.cloud.getTempFileURL({
                fileList: list.slice(0, 50),
                success: ress => {
                    wx.cloud.getTempFileURL({
                        fileList: list.slice(50, 100),
                        success: res => {
                            that.setData({
                                urlList: ress.fileList.concat(res.fileList),
                                roleloading: false
                            })
                            for (let i in this.data.role) {
                                this.data.role[i].url = this.data.urlList[i].tempFileURL
                                let arr = []
                                if (this.data.role[i].race.indexOf('-') >= 0){
                                    for (let j in this.data.role[i].race.split('-')) {
                                        arr.push(this.data.role[i].race.split('-')[j])
                                    }
                                }else{
                                    arr.push(this.data.role[i].race)
                                }
                                this.data.role[i].race = arr;
                            }
                            this.setData({
                                role: this.data.role,
                                showrole: this.data.role,
                                loading: false
                            })
                        },
                        fail: console.error
                    })

                },
                fail: console.error
            })
        } else {
            wx.cloud.getTempFileURL({
                fileList: list,
                success: res => {
                    that.setData({
                        urlList: res.fileList,
                        roleloading: false
                    })
                    for(let i in this.data.role){
                        this.data.role[i].url = this.data.urlList[i].tempFileURL
                    }
                    this.setData({
                        role: this.data.role,
                        showrole: this.data.role
                    })
                },
                fail: console.error
            })
        }
    },
    getEffect() {
        wx.cloud.callFunction({
            name: 'getEffect',
            success: res => {
                let classify1 = []
                let classify2 = []
                for (let i in res.result.data){
                    if (res.result.data[i].classify == 1){
                        classify1.push(res.result.data[i].name)
                    } else if (res.result.data[i].classify == 2){
                        classify2.push(res.result.data[i].name)
                    }
                }
                this.setData({
                    effect: res.result.data,
                    classify1: classify1,
                    classify2: classify2
                })
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '调用失败',
                })
                console.error('[云函数] [login] 调用失败：', err)
            }
        })
    },
    getRole() {
        this.setData({
            roleloading: true
        })
        wx.cloud.callFunction({
            name: 'getRole',
            success: res => {
                console.log(res)
                this.setData({
                    role: res.result.data.sort((a, b) => {
                        return a.price - b.price
                    })
                })
                let imglist = []
                for (let i in this.data.role) {
                    imglist.push(this.data.role[i].img)
                }
                this.setData({
                    imglist: imglist
                })
                this.getTempFileURL(imglist)
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '调用失败',
                })
                console.error('[云函数] [login] 调用失败：', err)
            }
        })
    },
    onLoad(){
        this.getRole()
        this.getEffect()
    },
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target,
            curHero: this.data.showrole[e.currentTarget.dataset.index]
        })
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    tolog(e){
        wx.navigateTo({
            url: '../../pages/updatalog/updatalog',
        })
    },
    toabout(){
        wx.navigateTo({
            url: '../../pages/about/about',
        })
    }
})